﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Forms;
using System.IO;
using System.Net;
using MobileInterviewer.pl.studentlive.nosoifighters;
using MobileInterviewer.pl.studentlive.nosoifighters1;

namespace MobileInterviewer
{
    public partial class StartForm : Form
    {
        private int imageID = -1;
        private double latitude = 0f;
        private double longitude = 0f;
        private int nodeID;
        private int netID = 1;//tymczasowo wpisane na stale

        public StartForm()
        {
            InitializeComponent();
        }

        private void sendNodeData()
        {
            string adress = countryTextBox.Text + townTextBox.Text + streetTextBox.Text;
            GraphProviderService gpService = new GraphProviderService();
            double[] position = gpService.GetGeoCode(adress);
            latitude = position[0];
            longitude = position[1];
            DataProviderService ws = new DataProviderService();
            nodeID = ws.NewNodeStr(nameTextBox.Text, surnameTextBox.Text, dateTimePicker1.Value, imageID, (float)this.latitude, (float)this.longitude, streetTextBox.Text, townTextBox.Text, postcodeTextBox.Text, countryTextBox.Text);
        }

        private void backMenuItem_Click(object sender, EventArgs e)
        {
            //Program.toHelloForm();
            Program.setState(Program.helloFormState);
        }

        private void nextMenuItem_Click(object sender, EventArgs e)
        {
            if (imageID == -1)
            {
                MessageBox.Show("take a photo first");
            }
            else
            {
                sendNodeData();
                //Program.toNodeList(nodeID, netID);
                Program.nodeID = nodeID;
                Program.netID = netID;
                Program.setState(Program.nodeListState);
            }
        }

        private void photoButton_Click(object sender, EventArgs e)
        {
            string filename = takePicture();
            if (filename != null)
            {
                imageID = uploadImage(filename);
            }
        }

        private void gpsButton_Click(object sender, EventArgs e)
        {
            //TO DO
            //set latitude and longitude
            
        }

        private int uploadImage(string path)
        {
            FileInfo fileInf = new FileInfo(path);
            System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);
            pl.studentlive.nosoifighters.DataProviderService ws = new MobileInterviewer.pl.studentlive.nosoifighters.DataProviderService();
            return ws.uploadImage(bytes);
        }

        private string takePicture()
        {
            string result = null;
            CameraCaptureDialog cameraDialog = new CameraCaptureDialog();
            cameraDialog.Mode = CameraCaptureMode.Still;
            cameraDialog.StillQuality = CameraCaptureStillQuality.High;
            cameraDialog.Resolution = new Size(640, 480);
            if (cameraDialog.ShowDialog() == DialogResult.OK)
            {
                result = cameraDialog.FileName;
            }
            this.Show();
            cameraDialog.Dispose();
            return result;
        }
    }
}